package com.android.aiod.ui.activity;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.android.baselibrary.interface_.CommCallBack;
import com.android.baselibrary.view.Comm_EditView;
import com.android.baselibrary.view.Comm_HeadView;
import com.android.baselibrary.view.MultiStateView;
import com.android.aiod.R;
import com.android.aiod.adapter.SerialPortListAdapter;
import com.android.aiod.event.Event_ReceiveData;
import com.android.aiod.manager.SerialConnectManager;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.licheedev.hwutils.ByteUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Arrays;

import butterknife.BindView;
import butterknife.OnClick;

public class SerialPortActivity extends BaseActivity {

    @BindView(R.id.multiplestatusView)
    MultiStateView multiplestatusView;
    @BindView(R.id.comm_title)
    Comm_HeadView comm_title;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.tv_receive)
    TextView tv_receive;
    @BindView(R.id.et_send)
    Comm_EditView et_send;


    SerialConnectManager serialConnectManager;
    SerialPortListAdapter adapter;
    String device;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_serialport;
    }

    @Override
    protected boolean isRegistEventbus() {
        return true;
    }

    @Override
    protected void initView() {
        comm_title.setTitle("串口设置");
        comm_title.setLeftClickFinish((Activity) mContext);

        adapter = new SerialPortListAdapter(mContext, callBack);
        recyclerview.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerview.setAdapter(adapter);

    }

    CommCallBack callBack = new CommCallBack() {
        @Override
        public void onResult(Object obj) {
            device = (String) obj;
        }
    };


    @Override
    protected void initData() {
        serialConnectManager = new SerialConnectManager(mContext);
        adapter.setData(Arrays.asList(serialConnectManager.mDevices));

    }

    @OnClick({R.id.tv_open, R.id.tv_send})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_open:  //打开串口
                if (TextUtils.isEmpty(device)) {
                    ToastUtils.showShort("请先选择一个串口");
                    return;
                }
                serialConnectManager.switchSerialPort(device);
                break;
            case R.id.tv_send:
                String text = et_send.getText();
                if (TextUtils.isEmpty(text)) {
                    ToastUtils.showShort("请输入内容");
                    return;
                }
                serialConnectManager.sendData(text);
                break;
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(Event_ReceiveData event) {
//        String hexStr = ConvertUtils.bytes2String(event.received);
        String hexStr = ByteUtil.bytes2HexStr(event.received, 0, event.size);
//        CommToast.showToast(mContext, hexStr);

        tv_receive.setText(hexStr + "\n");

    }

}
